package com.tiyende.kash.loop.utils.helper

import com.tiyende.kash.loop.configs.TiyendeApiUrl
import com.tiyende.kash.loop.request.HttpHelper
import com.tiyende.kash.loop.request.IHttpCallback
import org.json.JSONObject


/**
 * 检查参数是否正确
 */
object TiyendeCheckParamsHelper {

    fun onCheckParamsAction(params: JSONObject,callBack: (ArrayList<String>) -> Unit){
        val httpUrl = TiyendeApiUrl.getTiyendeHttpUrl()
        HttpHelper.post(httpUrl, params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                when(data.optInt("code")) {
                    0 -> {//成功
                        val array = data.optJSONArray("obj")
                        array?.let {
                            val list = arrayListOf<String>()
                            for (i in 0..<it.length()){
                                list.add(it.optString(i))
                            }
                            if (list.isNotEmpty()){
                                callBack.invoke(list)
                            }
                        }
                    }
                }
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
            }
        })
    }
}